﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Commons;
using Core;
using Core.Factory;
using Microsoft.AspNetCore.Mvc;
using Model;
using NLog;
using WebApp.Models;
using WebApp.ViewModel;

namespace WebApp.ApiControllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    
    public class UserController : Controller
    {
        private readonly IBaseService _baseService;
        private readonly IUserService _userService;

        private readonly Logger log = LogManager.GetCurrentClassLogger();
        private readonly string _snsUrl= MyConfigurationSection.GetSite("SnsUrl");
        /// <summary>
        /// 用户信息
        /// </summary>
        /// <param name="baseService"></param>
        /// <param name="userService"></param>
        public UserController(IBaseService baseService, IUserService userService)
        {
            _baseService = baseService;
            _userService = userService;
        }
        /// <summary>
        /// Get
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        /// <summary>
        /// Get
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public ResultModel Get(int id)
        {
            ResultModel model = new ResultModel();
            var user = _baseService.GetModel<T_User>(i => i.Id == id);
            int re = 0;
            if (user.ExpireDate.HasValue)
            {
                if ((DateTime.Now - user.ExpireDate.Value).TotalSeconds < 0)
                {
                    re = 1;
                }
            }
            user.IsVip = re;
            model.code = "200";
            model.data = user;
            model.msg = "获取成功";
            model.result = (int)MsgType.success;
            model.type = MsgType.success.ToString();

            return model;
        }

        /// <summary>
        /// Post
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel Post(UserModel user)
        {
            ResultModel resultModel = new ResultModel();
            string AppID = MyConfigurationSection.GetSite("AppID");
            string Secret = MyConfigurationSection.GetSite("Secret");

            HttpClient client = new HttpClient();
            HttpResponseMessage response = client.GetAsync(_snsUrl + AppID + "&secret=" + Secret + "&js_code=" + user.code + "&grant_type=authorization_code").Result;
            string result = response.Content.ReadAsStringAsync().Result;
            client.Dispose();
            Code2SessionModel code2 = Newtonsoft.Json.JsonConvert.DeserializeObject<Code2SessionModel>(result);

            if (string.IsNullOrWhiteSpace(code2.errcode))
            {
                T_User model = new T_User();
                model.Sex = user.gender;
                model.WxName = user.nickName;
                model.StateCode = StateCode.Enable.ToString();
                model.WxOpenId = code2.openid;
                model.WxPhoto = user.avatarUrl;

                model = _userService.LoginValidate(model);

                resultModel.result = (int)MsgType.success;
                resultModel.code = "200";
                resultModel.msg = "登录成功";
                resultModel.data = model;
                resultModel.type = MsgType.success.ToString();
            }
            else
            {
                resultModel.result = (int)MsgType.error;
                resultModel.msg = code2.errmsg;
                resultModel.code = code2.errcode;
            }
            return resultModel;


        }


        /// <summary>
        /// Put
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]string value)
        {
        }

        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="id"></param>
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
